// app.js
const loginService = require('service/loginService.js')
const utils = require('utils/util.js')
// load promise 
const request = require('utils/wx-pro.js')

App({
  onLaunch: function (options) {
    // 调用API从本地缓存中获取数据  
    var that = this
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    /*if (!wx.getStorageSync('sessionId')) {
      wx.navigateTo({
        url: '../login/login'
      })
    }*/
    request.setHttpCallBack((data) => {
      console.log(data)
      if (data.code == 500 && data.message == '未登录' || data.message == 'not_login') {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        var url = currentPage.route    //当前页面url
        console.log(url)
        wx.setStorageSync('redirectUrl', url)
        wx.navigateTo({
          url: '../login/login'
        })
      }
      if (data.code == 500 && data.message == 'new_user'){
        console.log("new_user")
        wx.setStorageSync('sessionId', data.response.sessionId)
        wx.navigateTo({
          url: '../register/register'
        })
      }
    });
  },  
  onShow: function (options) {
    
  },  
  globalData:{
    userInfo: null 
  }
})